package com.ruoyi.web.controller.process;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.process.ProcessAluminized;
import com.ruoyi.system.domain.process.ProcessInventory;
import com.ruoyi.system.domain.process.ProcessRibbon;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.processService.IProcessAluminizedService;
import com.ruoyi.system.service.processService.IProcessInventoryService;
import com.ruoyi.system.service.processService.IProcessRibbonService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 加工镀铝Controller
 * 
 * @author ljx
 * @date 2020-07-09
 */
@Controller
@RequestMapping("/process/aluminized")
public class ProcessAluminizedController extends BaseController
{
    private String prefix = "process/aluminized";

    @Resource
    private IProcessAluminizedService processAluminizedService;
    @Resource
    private ISysUserService iSysUserService;
    @Resource
    private IProcessRibbonService iProcessRibbonService;

    @RequiresPermissions("process:aluminized:view")
    @GetMapping()
    public String aluminized()
    {
        return prefix + "/aluminized";
    }

    /**
     * 查询加工镀铝列表
     */
    @RequiresPermissions("process:aluminized:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcessAluminized processAluminized)
    {
        startPage();
        List<ProcessAluminized> list = processAluminizedService.selectProcessAluminizedList(processAluminized);
        return getDataTable(list);
    }

    /**
     * 导出加工镀铝列表
     */
    @RequiresPermissions("process:aluminized:export")
    @Log(title = "加工镀铝", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcessAluminized processAluminized)
    {
        List<ProcessAluminized> list = processAluminizedService.selectProcessAluminizedList(processAluminized);
        list.stream().forEach(aluminized -> {
            aluminized.setSurfaceName(aluminized.getSurface()==1?"内":"外");
        });
        ExcelUtil<ProcessAluminized> util = new ExcelUtil<ProcessAluminized>(ProcessAluminized.class);
        return util.exportExcel(list, "aluminized");
    }

    /**
     * 新增加工镀铝
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SysUser> users = iSysUserService.getAllUser();
        ProcessRibbon ribbon = new ProcessRibbon();
        ribbon.setType(1);
        List<ProcessRibbon> ribbons = iProcessRibbonService.selectRibbonAndInventory(ribbon);
        //操作人
        mmap.put("users",users);
        //合并材料编号
        mmap.put("ribbons",ribbons);
        return prefix + "/add";
    }

    /**
     * 新增保存加工镀铝
     */
    @RequiresPermissions("process:aluminized:add")
    @Log(title = "加工镀铝", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcessAluminized processAluminized)
    {
        processAluminized.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(processAluminizedService.insertProcessAluminized(processAluminized));
    }

    /**
     * 修改加工镀铝
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        //通过镀铝id查询到镀铝对应的原膜数据
        List<ProcessInventory> list = processAluminizedService.getProcessInventory(id);
        //验证是否有分卷状态的原膜
        List<ProcessInventory> collect = list.stream().filter(l -> l.getStatus() > 3).collect(Collectors.toList());
        List<SysUser> users = iSysUserService.getAllUser();
        mmap.put("users",users);
        mmap.put("inventorys",list);
        ProcessAluminized processAluminized = processAluminizedService.selectProcessAluminizedById(id);
        if(processAluminized.getSurface()==1){
            processAluminized.setSurfaceName("内");
        }else if(processAluminized.getSurface()==2){
            processAluminized.setSurfaceName("外");
        }else if(processAluminized.getSurface()==3){
            processAluminized.setSurfaceName("内+外");
        }
        mmap.put("processAluminized", processAluminized);
        if(CollectionUtils.isNotEmpty(collect)){
            //存在分卷原膜，只提供修改基础信息
            return prefix + "/edit";
        }
        //获得到该镀铝绑定的并卷
        List<ProcessRibbon> ribbons1 = iProcessRibbonService.selectByAluminizedId(id);
        //获得不在镀铝列表内的并卷
        List<ProcessRibbon> ribbons = iProcessRibbonService.selectRibbonAndInventory2(null);
        ribbons.addAll(ribbons1);
        //合并材料编号
        mmap.put("ribbons",ribbons);
        return prefix + "/canEdit";
    }

    /**
     * 修改保存加工镀铝
     */
    @RequiresPermissions("process:aluminized:edit")
    @Log(title = "加工镀铝", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProcessAluminized processAluminized)
    {
        processAluminized.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(processAluminizedService.updateProcessAluminized(processAluminized));
    }

    /**
     * 删除加工镀铝
     */
    @RequiresPermissions("process:aluminized:remove")
    @Log(title = "加工镀铝", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return processAluminizedService.deleteProcessAluminizedByIds(ids);
    }
}
